qx.Class.define("elpintao_comp.comp.clientes.windowCliente",
{
	extend : controles.window.Window,
	construct : function (appMain, id_cliente)
	{
		this.base(arguments);

	this.set({
		width: 350,
		height: 320,
		showMinimize: false,
		showMaximize: false
	});
	this.setLayout(new qx.ui.layout.Canvas());
	
	this.addListenerOnce("appear", function(){
		txtRazonSocial.focus();
	});
	
	
	var modelForm = null;
	
	
	var form = new qx.ui.form.Form();
	
	var txtRazonSocial = new qx.ui.form.TextField("");
	txtRazonSocial.setRequired(true);
	txtRazonSocial.setWidth(230);
	form.add(txtRazonSocial, "Razón social", null, "razon_social");
	
	var rbgTipo_persona = new qx.ui.form.RadioButtonGroup();
	rbgTipo_persona.setLayout(new qx.ui.layout.HBox(5));
	var rbt1 = new qx.ui.form.RadioButton("Física");
	rbt1.setModel("F");
	var rbt2 = new qx.ui.form.RadioButton("Jurídica");
	rbt2.setModel("J");
	rbgTipo_persona.add(rbt1);
	rbgTipo_persona.add(rbt2);
	form.add(rbgTipo_persona, "Tipo", null, "tipo");
	
	var slbResp_iva = new qx.ui.form.SelectBox();
	slbResp_iva.add(new qx.ui.form.ListItem("Resp. inscrip.", null, "I"))
	slbResp_iva.add(new qx.ui.form.ListItem("Consumidor final", null, "C"))
	slbResp_iva.add(new qx.ui.form.ListItem("Monotributo", null, "M"))
	slbResp_iva.add(new qx.ui.form.ListItem("Exento", null, "E"))
	slbResp_iva.add(new qx.ui.form.ListItem("No responsable", null, "A"))
	form.add(slbResp_iva, "Resp. IVA", null, "condicion_iva");
	
	var slbTipo_doc = new qx.ui.form.SelectBox();
	slbTipo_doc.add(new qx.ui.form.ListItem("C.U.I.T.", null, "C"));
	slbTipo_doc.add(new qx.ui.form.ListItem("C.U.I.L.", null, "L"));
	slbTipo_doc.add(new qx.ui.form.ListItem("D.N.I.", null, "2"));
	slbTipo_doc.add(new qx.ui.form.ListItem("Lib. enrolam.", null, "0"));
	slbTipo_doc.add(new qx.ui.form.ListItem("Lib. cívica", null, "1"));
	slbTipo_doc.add(new qx.ui.form.ListItem("Pasaporte", null, "3"));
	slbTipo_doc.add(new qx.ui.form.ListItem("Cédula ident.", null, "4"));
	form.add(slbTipo_doc, "Tipo doc.", null, "tipo_doc");
	
	var txtNrodoc = new qx.ui.form.TextField("");
	txtNrodoc.setRequired(true);
	txtNrodoc.addListener("blur", function(e){
		txtNrodoc.setValue(qx.lang.String.trim((txtNrodoc.getValue())));
	});
	form.add(txtNrodoc, "Nro.doc.", null, "dni_cuit");
	
	var txtDomicilio = new qx.ui.form.TextField("");
	form.add(txtDomicilio, "Domicilio", null, "domicilio");
	
	var txtTelefono = new qx.ui.form.TextField("");
	form.add(txtTelefono, "Teléfono", null, "telefono");
	
	var formView = new qx.ui.form.renderer.Single(form);
	
	this.add(formView, {left: 0, top: 0})
	
	var controllerForm = new qx.data.controller.Form(null, form);
	
	var validationManager = form.getValidationManager();

	validationManager.setValidator(function(items) {
		var bandera = true;
		var modelSelection = slbTipo_doc.getModelSelection().getItem(0);
		if (slbResp_iva.getModelSelection().getItem(0)=="I" && modelSelection != "C") {
			slbTipo_doc.setInvalidMessage("Para Resp. inscrip. corresponde C.U.I.T. como Tipo doc.")
			slbTipo_doc.setValid(false);
			bandera = false;
		} else {
			slbTipo_doc.setValid(true);
		}
		if ((modelSelection == "C" || modelSelection == "L") && ! appMain.functionValidarCuit(txtNrodoc.getValue())) {
			txtNrodoc.setInvalidMessage("Nro. C.U.I.T./C.U.I.L. no válido")
			txtNrodoc.setValid(false);
			bandera = false;
		}
		
		return bandera;
	});
	
	
	
	
	if (id_cliente==null) {
		this.setCaption("Alta de cliente");
		modelForm = controllerForm.createModel(true);
	} else {
		this.setCaption("Modificación de cliente");
		
		var rpc = new qx.io.remote.Rpc("services/", "elpintao.Clientes");
		try {
			var resultado = rpc.callSync("leer_clientes", {id_cliente: id_cliente});
		} catch (ex) {
			alert("Sync exception: " + ex);
		}
		
		modelForm = qx.data.marshal.Json.createModel(resultado[0]);
		controllerForm.setModel(modelForm);
	}
	
	


	var commandEsc = new qx.ui.core.Command("Esc");
	this.registrarCommand(commandEsc);
	commandEsc.addListener("execute", function(e){
		btnCancelar.execute();
	});
	
	var btnAceptar = new qx.ui.form.Button("Aceptar");
	btnAceptar.addListener("execute", function(e){
		if (form.validate()) {
			var p = qx.util.Serializer.toNativeObject(modelForm);
			if (id_cliente==null) p.id_cliente = "0";
	
			var rpc = new qx.io.remote.Rpc("services/", "elpintao.Clientes");
			try {
				var resultado = rpc.callSync("alta_modifica_cliente", p);
			} catch (ex) {
				alert("Sync exception: " + ex);
			}
			
			if (resultado.error.length > 0) {
				dialog.Dialog.error(resultado.error[0].message, function(e){
					slbTipo_doc.focus();
				});
			} else {
				appMain.functionTransmitir();
				this.fireDataEvent("aceptado", resultado.id_cliente);
				btnCancelar.execute();				
			}
		} else {
			var items = form.getItems();
			for (var item in items) {
				if (!items[item].isValid()) {
					items[item].focus();
					break;
				}
			}			
		}
	}, this);

	var btnCancelar = new qx.ui.form.Button("Cancelar");
	btnCancelar.addListener("execute", function(e){
		this.destroy();
	}, this);
	
	this.add(btnAceptar, {left: 60, bottom: 0});
	this.add(btnCancelar, {left: 200, bottom: 0});
		
		
		
	},
	members : 
	{

	}
});